//
//  ViewManager.m
//  iMarks
//
//  Created by Christian Speich on 20.12.07.
//  Copyright 2007-2008 Christian Speich. All rights reserved.
//

#import <UIKit/UIView.h>
#import "ViewManager.h"

#define NotResponds(object,selector) NSLog(@"%@ dont responds to %@", \
										NSStringFromClass([object class]), \
										NSStringFromSelector(selector));

@implementation ViewManager

- (id) initWithTransitionView:(id)view andNavigationBar:(id)_bar 
				  andMainView:(id)view2;
{
	self = [super init];
	if (self != nil) {
		transView = [view retain];
		bar = [_bar retain];
		views = [[NSMutableArray alloc] initWithCapacity:1];
		mainView = [view2 retain];
		
		[bar setDelegate:self];
	}
	return self;
}

- (void)dealloc
{
	[views release];
	[transView release];
	[bar release];
	[mainView release];
	
	[super dealloc];
}

- (id)currentView
{
	if ([views count] > 0) {
		return [[views lastObject] retain];
	} else {
		return nil;
	}
}

- (id)lastView
{
	if ([views count] > 1) {
		return [[views objectAtIndex:([views count]-2)] retain];
	}
	else {
		return nil;
	}
}

- (id)bar
{
	return [bar retain];
}

- (id)mainView
{
	return [mainView retain];
}

- (void)addView:(id)view
{
	[self addView:view withNavigationItem:nil];
}

- (void)addView:(id)view withNavigationItem:(UINavigationItem*)item
{
	SEL selector;
	if (view == nil) {
		return;
	}
	
	if (![view isKindOfClass:[UIView class]]) {
		return;
	}
	
	[views addObject:view];
	
	if ([views count] == 0) {
		[transView addSubview:view];
	}
	else {
		[transView transition:1 toView:view];
		
		selector = @selector(becomesManager:);
		if ([view respondsToSelector:selector]) {
			[view performSelector:selector withObject:self];
		}
		else {
			NotResponds(view, selector);
		}
	}
	
	selector = @selector(showBarButtons);
	if ([view respondsToSelector:selector]) {
		[view performSelector:selector];
	}
	else {
		NotResponds(view, selector);
		[bar showButtonsWithLeft:nil right:nil leftBack:YES];
	}
	
	if (item != nil) {
		[bar pushNavigationItem:item];
	}
	else {
		selector = @selector(NavigationItem);
		if ([view respondsToSelector:selector]) {
			[bar pushNavigationItem:[view performSelector:selector]];
		}
		else {
			NotResponds(view, selector);
			[bar pushNavigationItem:
			 [[[UINavigationItem alloc] initWithTitle:@""] autorelease]];
		}
	}
}

- (void)back
{
	[bar popNavigationItem];
}

#pragma mark ----------Delegate Methods-----------

- (void)navigationBar:(UINavigationBar*)_bar buttonClicked:(int)button
{
	SEL selector = @selector(navigationBar: buttonClicked:);
	id currentView = [self currentView];
	
	if ([[self currentView] respondsToSelector:selector]) {
		[[self currentView] performSelector:selector withObject:_bar 
								 withObject:(id)button];
	}
	else {
		NotResponds(currentView, selector);
	}
	[currentView release];
}

- (void) navigationBar:(UINavigationBar*)_bar poppedItem:(UINavigationItem*)item
{
	SEL selector;
	id lastView = [self lastView];
	id currentView = [self currentView];
	
	selector = @selector(becomesBack);
	if ([lastView respondsToSelector:selector]) {
		[lastView performSelector:selector];
	}
	else {
		NotResponds(currentView, selector);
	}
	
	[transView transition:2 toView:lastView];
	[views removeLastObject];
	[currentView release];
	
	selector = @selector(showBarButtons);
	if ([lastView respondsToSelector:selector]) {
		[lastView performSelector:selector];
	}
	else {
		NotResponds(currentView, selector);
		[bar showButtonsWithLeft:nil right:nil leftBack:YES];	
	}
	
	[lastView release];
}

@end
